package group

import (
	"gitee.com/xinshortlink/internal/dto"
	"gitee.com/xinshortlink/internal/dto/resp"
	"gitee.com/xinshortlink/internal/service/group"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type Api struct {
	logger  *zap.Logger
	service *group.Service
	resp    *resp.Util
}

func NewApi(logger *zap.Logger, service *group.Service, resp *resp.Util) *Api {
	return &Api{
		logger:  logger,
		service: service,
		resp:    resp,
	}
}

func (api *Api) Create(ctx *gin.Context) {
	req := &dto.CreateGroupReq{}
	if err := ctx.ShouldBindJSON(req); err != nil {
		api.resp.Resp(ctx, err, nil)
		return
	}

	err := api.service.Create(ctx.Request.Context(), req.Name)
	api.resp.Resp(ctx, err, nil)
}

func (api *Api) Groups(ctx *gin.Context) {
	groups, err := api.service.Groups(ctx.Request.Context())
	api.resp.Resp(ctx, err, groups)
}

func (api *Api) Update(ctx *gin.Context) {
	req := &dto.UpdateGroupReq{}
	if err := ctx.ShouldBindJSON(req); err != nil {
		api.resp.Resp(ctx, err, nil)
		return
	}

	err := api.service.Update(ctx.Request.Context(), req.Id, req.Name)
	api.resp.Resp(ctx, err, nil)
}

func (api *Api) Del(ctx *gin.Context) {
	req := &dto.DelGroupReq{}
	if err := ctx.ShouldBindQuery(req); err != nil {
		api.resp.Resp(ctx, err, nil)
		return
	}

	err := api.service.Del(ctx.Request.Context(), req.Id)
	api.resp.Resp(ctx, err, nil)
}
